package ch.fhnw.qualiscan.application.components;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.ForgotPassword;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.core.LayoutComponent;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.UserSession;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;
import ch.fhnw.qualiscan.core.libraries.ModalLibrary;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.LoginForm.LoginListener;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;
/**
 * Login Box Component
 * @author Kevin Mettenberger
 *
 */
@SuppressWarnings("serial")
public class LoginBox extends LayoutComponent implements LoginListener {

	//private LoginFormElement login;
	private LoginForm loginForm;
	public SurveyUserRole invitationSurvey;
	public boolean updateSur;
	
	Logger logger = LoggerFactory.getLogger("LoginBox");
	
	ModalLibrary modalLib = new ModalLibrary();
	private UserModel userModel = new UserModel();
	private SurveyModel surveyModel = new SurveyModel();
	private User user = new User();
	private UserSession userSession = new UserSession();
	private UserSessionHelper userSessionHelper = new UserSessionHelper();
	
	private Label statusText;

	public LoginBox() {}
	
	public LoginBox(SurveyUserRole invitationSurvey, boolean updateSur) {
		this.invitationSurvey = invitationSurvey;
		this.updateSur = updateSur;
		removeAllComponents();
		buildComponent();
	}
	
	@Override
	public void buildComponent() {
		
		CustomLayout custom = new CustomLayout("custom/sidebarComponent");
		addComponent(custom);
		
		custom.addComponent(textHelper.boxTitle(TM.get("loginbox.title")), "title");
		
		VerticalLayout boxContent = new VerticalLayout();
		boxContent.addComponent(textHelper.p(TM.get("loginbox.desc")));
		loginForm = new CustomLoginForm();
		loginForm.addListener(this);
		boxContent.addComponent(loginForm);
		
		statusText = new Label("");
		statusText.setStyleName("error");
		statusText.setVisible(false);
		statusText.setValue(TM.get("login.incorrect"));
		boxContent.addComponent(statusText);
		
		
		Button forgotPasswordButton = new Button(TM.get("login.forgot"));
		forgotPasswordButton.setStyleName(Button.STYLE_LINK);
		forgotPasswordButton.setIcon(Config.ICON_KEY);
		boxContent.addComponent(forgotPasswordButton);
		
		forgotPasswordButton.addListener(new ClickListener() {
			
			public void buttonClick(ClickEvent arg0) {
				modalLib.openModal(new ForgotPassword());
				
			}
		});
		custom.addComponent(boxContent, "content");
		
	}

	public void onLogin(LoginEvent event) {
		user = userModel.login(event.getLoginParameter("username"), event.getLoginParameter("password"));
		
		
		if (user == null) {
			loginIncorrect();
		}
		else {
			if (updateSur == true) {
				invitationSurvey.setUser(user);
				surveyModel.updateSur(invitationSurvey);
			}
			
			loginCorrect();
		}
	}

	private void loginCorrect() {
		logger.info("Login Correct.");
		
		userSession.setEmail(user.getEmail());
		userSession.setFirstname(user.getFirstname());
		userSession.setLastname(user.getLastname());
		userSession.setId(user.getId());
		userSession.setLanguage(user.getLanguage());
		userSession.setRole(user.getRole());

		
		sessionHelper.setUserSession(userSession);
		
		//Change Layout
		componentLib.userLoggedIn();
		
				
		if(userSession.getRole().getId() == Config.ADMINISTRATOR_ID) {
			loaderLib.setUrlFragment(0, "AdminPanel");
		} else {
			loaderLib.setUrlFragment(0, "Dashboard");
		}
		
		
	}

	private void loginIncorrect() {
		logger.info("Login Correct.");
		
		statusText.setVisible(true);
		// TODO Auto-generated method stub
		
	}
	
	
	

//	@Override
//	public void buildComponent() {
//
//		CustomLayout custom = new CustomLayout("custom/sidebarComponent");
//		addComponent(custom);
//		
//		if(sessionHelper.isUserSessionSet() == false) {
//			login = new LoginFormElement(this);
//			
//			custom.addComponent(textHelper.boxTitle(TM.get("loginbox.title")), "title");
//			
//			VerticalLayout boxContent = new VerticalLayout();
//			boxContent.addComponent(textHelper.p(TM.get("loginbox.desc")));
//			boxContent.addComponent(login);
//			custom.addComponent(boxContent, "content");
//		} else {
//			custom.addComponent(textHelper.boxTitle(TM.get("loginbox.title")), "title");
//			
//			VerticalLayout boxContent = new VerticalLayout();
//			boxContent.addComponent(textHelper.p(TM.get("loginbox.loggedin")));
//			Button dashBoard = new Button(TM.get("loginbox.dashboard"));
//			
//			dashBoard.addListener(new ClickListener() {
//				public void buttonClick(ClickEvent event) {
//					//urlLib.setPage("Dashboard");
//				}
//			});
//			dashBoard.setIcon(Config.ICON_GOTO);
//			
//			boxContent.addComponent(dashBoard);
//			
//			custom.addComponent(boxContent, "content");
//		}
//	}

//	public void setUsername(String email) {
//		login.setUsername(email);
//	}

	
	
	
}
